package com.objectproject.main.service.subsystem.loader;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.objectproject.main.dao.MyObjectDAOImpl;
import com.objectproject.main.dao.MyPrimitiveDAOImpl;
import com.objectproject.main.enumeration.ValueType;
import com.objectproject.main.model.MyObject;
import com.objectproject.main.model.MyProperty;
import com.objectproject.main.model.primitive.MyPrimitiveCollection;

@Service
public class ValueLoader {
	
	@Autowired
	MyObjectDAOImpl myObjectDAOImpl;
	
	@Autowired
	MyPrimitiveDAOImpl myPrimitiveDAOImpl;
	
	public void loadAllPropertyValuesOf(MyObject myObject, int localeId){
		
		
		ArrayList<MyProperty> myProperties = myObject.getMyProperties();
		MyProperty currentProperty = null;
		ValueType currentValueType = null;
		long currentPropertyValueId = 0L;
		Object currentValue = null;
		
		for(int i = 0 ; i < myProperties.size() ; i++){
			currentProperty = myProperties.get(i);
			currentValueType = currentProperty.getTypeOfValue();
			currentPropertyValueId = currentProperty.getValueId();
			
			if(currentPropertyValueId == 0L){//속성 값이 정의되어있지 않다면
				continue;
			}
			
			currentValue = loadPropertyValueEasy(currentValueType, currentPropertyValueId);
			myProperties.get(i).setValue(currentValue);
		}
	}
	
	/**
	 * helper of loading method
	 * @param myObject
	 */
	public Object loadPropertyValueEasy(ValueType valueType, long valueId){
		//if objectId or primitiveId is zero, it will return null.
		if(valueType == ValueType.INTEGER){
			return myPrimitiveDAOImpl.selectMyPrimitiveInteger(valueId);
		}else if(valueType == ValueType.TEXT){
			return myPrimitiveDAOImpl.selectMyPrimitiveText(valueId);
		}else if(valueType == ValueType.COLLECTION){
			return myPrimitiveDAOImpl.selectMyPrimitiveCollection(valueId);
		}else if(valueType == ValueType.DATE){
			return myPrimitiveDAOImpl.selectMyPrimitiveDate(valueId);
		}else if(valueType == ValueType.TIME){
			return myPrimitiveDAOImpl.selectMyPrimitiveTime(valueId);
		}else if(valueType == ValueType.IMAGE){
			return myPrimitiveDAOImpl.selectMyPrimitiveImage(valueId);
		}else if(valueType == ValueType.REAL){
			return myPrimitiveDAOImpl.selectMyPrimitiveReal(valueId);
		}else if(valueType == ValueType.LOCATION){
			
		}else if(valueType == ValueType.SOUND){
			
		}else if(valueType == ValueType.VIDEO){
			
		}else if(valueType == ValueType.OBJECT){
			return myObjectDAOImpl.selectMyObjectById(valueId);
		}
		return null;
	}
	
	/**
	 * set only primitive value of primitive-property
	 * @param myObject
	 */
	public void loadPrimitivePropertyValuesOf(MyObject myObject){
		
	}
	
	public void loadMinimizedPropertyValuesOf(){
		
	}
	
	public void loadSimplifiedPropertyValuesOf(){
		
	}
	
	public void loadCardPropertyValuesOf(){
		
	}
	
	public void loadCollectionElementValuesOf(MyPrimitiveCollection myPrimitiveCollection){
		
	}
}
